package com.lcy.redismessage.service;
import com.lcy.redismessage.domain.vo.RedisQueueMsg;
import org.redisson.api.RBoundedBlockingQueue;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * @创建人:yewei
 * @创建时间: 2021/11/21 16:17
 * @描述
 */
@Service
public class RedissonService {

    @Autowired
    RedissonClient redissonClient;


    public RBoundedBlockingQueue<RedisQueueMsg> getBoundedBlockingQueue(){
        RBoundedBlockingQueue<RedisQueueMsg> queue = redissonClient.getBoundedBlockingQueue("anyQueue");

        // 如果初始容量（边界）设定成功则返回`真（true）`，
        // 如果初始容量（边界）已近存在则返回`假（false）`。
        queue.trySetCapacity(10);
      //  queue.offer(new RedisQueueMsg(UUID.randomUUID().toString(),"推送消息01"));
      //  queue.offer(new RedisQueueMsg(UUID.randomUUID().toString(),"推送消息02"));
        // 此时容量已满，下面代码将会被阻塞，直到有空闲为止。
      //  queue.put(new SomeObject());
     //   SomeObject obj = queue.peek();
      //  SomeObject someObj = queue.poll();
      //  SomeObject ob = queue.poll(10, TimeUnit.MINUTES);
        return queue;
    }


    public void  getQueueVal(){
        RBoundedBlockingQueue<RedisQueueMsg> queue = redissonClient.getBoundedBlockingQueue("anyQueue");
        queue.trySetCapacity(10);
        // 此时容量已满，下面代码将会被阻塞，直到有空闲为止。
        //   SomeObject obj = queue.peek();
      //  RedisQueueMsg poll = queue.poll();
        RedisQueueMsg poll = null;
        try {
            poll = queue.take();
            System.out.println(poll);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
